/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.beActionAssemblerFactory;

import com.inspur.edp.bef.api.action.assembler.IBEActionAssembler;
import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.be.BEMethod;
import com.inspur.edp.bef.dtgenerator.api.beActionAssemblerGen.CustomBEActionMethodGenerator;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.bef.dtgenerator.core.beActionAssembler.CustomBEActionAssGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import java.lang.reflect.Field;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class CustomBEActionMethodImplGenerator extends ClassMethodGenerator {
  private BefCoreGenCtx coreGenCtx;
  private BizAction action;
  private BEMethod method;

  public CustomBEActionMethodImplGenerator(BefCoreGenCtx coreGenCtx, BizAction action) {
    this.coreGenCtx = coreGenCtx;
    this.action = action;
  }

  @Override
  protected void doInitialize() {
    GspMetadata metadata =
        coreGenCtx
            .getBeGenCtx()
            .getMetadataPool()
            .getMetadata(action.getComponentPkgName(), action.getComponentId());

    GspComponent comp = (GspComponent) metadata.getContent();
    if (!comp.getComponentType().equals("BEComponent")) {
      throw new RuntimeException("错误的类型：" + comp.getComponentType());
    }
    method = (BEMethod) comp.getMethod();
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>(
        java.util.Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getMethodName() {
    return CustomBEActionMethodGenerator.getMethodName(action);
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return CustomBEActionMethodGenerator.getMethodParameters(method, coreGenCtx.getBeGenCtx());
  }

  @Override
  protected TypeInfo getReturnType() {
    return Utils.createTypeInfo(IBEActionAssembler.class);
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    ClassInstanceCreation creationExpression = ast.newClassInstanceCreation();
    creationExpression.setType(ast.newSimpleType(ast.newSimpleName(getAssemblerClassName(action))));
    for (ParameterInfo formalPar :
        CustomBEActionMethodGenerator.getMethodParameters(method, coreGenCtx.getBeGenCtx())) {
        creationExpression.arguments().add(ast.newSimpleName(formalPar.getParamName()));
    }
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(creationExpression);
    block.statements().add(returnStatement);
    return block;
    //		java.util.ArrayList<SyntaxNodeOrToken> pars = new java.util.ArrayList<SyntaxNodeOrToken>();
//		for ( ParameterInfo formalPar : CustomBEActionMethodGenerator.getMethodParameters(method,
    // coreGenCtx.getBeGenCtx()))
    //		{
    //			if(pars.Any())
    //			{
    //				pars.add(SyntaxFactory.Token(SyntaxKind.CommaToken));
    //			}
    //			pars.add(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(formalPar.ParameterName)));
    //		}
    //		return SyntaxFactory.Block(
    //				SyntaxFactory.<StatementSyntax>SingletonList(
    //						SyntaxFactory.ReturnStatement(
    //								SyntaxFactory.ObjectCreationExpression(
    //										SyntaxFactory.IdentifierName(GetAssemblerClassName(action))).WithArgumentList(
    //
    //	SyntaxFactory.ArgumentList(SyntaxFactory.<ArgumentSyntax>SeparatedList(pars.toArray(new
    // SyntaxNodeOrToken[]{})))))));
  }

  private String getAssemblerClassName(BizAction action) {
    return CustomBEActionAssGenerator.getItemName(action);
  }

  @Override
  protected java.util.ArrayList<BaseGenerator> createChildGenerators() {
    java.util.ArrayList<BaseGenerator> result = new java.util.ArrayList<BaseGenerator>(1);
    result.add(new CustomBEActionAssGenerator(coreGenCtx, action));
    return result;
  }
}
